package com.example.backend.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
    Integer     id;
    String      name;
    String      desc;
    Integer     stock;
    Double      price;
    String      typename;
    Integer     typeid;
    Boolean     tokendown;
    String      filename;
    String      base64;

    public void loadImage() throws IOException {
        File file = new File("uploads", filename);

        FileInputStream inputStream = new FileInputStream(file);

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte [] b = new byte[1024];
        int len = inputStream.read(b);
        while(len != -1) {
            byteArrayOutputStream.write(b, 0, len);
            len = inputStream.read(b);
        }

        byte [] filebyte = byteArrayOutputStream.toByteArray();
        // base64 = "data:image;base64," + new String(Base64.encodeBase64(filebyte));
        base64 = "data:image;base64," + new String(Base64.getEncoder().encode(filebyte));
    }
}
